/*
 * Group 3, User Interface Programming II (vt11)
 * Pooya | Flora | Monica | Can
 */
package vical.view;

import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import vical.controller.EntryController;
import vical.model.Internationalization;

/**
 * This is the panel that contains all existing categories of
 * entries added in this calendar.
 *
 * @author Can
 */
public class CategoryPanel extends JPanel implements ActionListener {

    private PriorityCheckBox highPriorityCheckBox;
    private PriorityCheckBox normalPriorityCheckBox;
    private ArrayList<JCheckBox> categoryCheckBoxes;
    private ViewPanel viewPanel;

    public CategoryPanel(ViewPanel viewPanel) {
        this.viewPanel = viewPanel;
        this.categoryCheckBoxes = new ArrayList<JCheckBox>();
        initComponents();
    }

    private void initComponents() {

        highPriorityCheckBox = new PriorityCheckBox("High", true);
        highPriorityCheckBox.addActionListener(this);
        highPriorityCheckBox.setToolTipText(Internationalization.getString("tooltip_high_priority"));

        normalPriorityCheckBox = new PriorityCheckBox("Normal", true);
        normalPriorityCheckBox.addActionListener(this);
        normalPriorityCheckBox.setToolTipText(Internationalization.getString("tooltip_normal_priority"));

        this.setLayout(new BoxLayout(this, WIDTH));
        this.add(highPriorityCheckBox);
        this.add(normalPriorityCheckBox);

        ArrayList<String> categories = EntryController.getCategories();
        for (String category : categories) {
            JCheckBox categoryCheckbox = new CategoryCheckBox(category, true);
            categoryCheckbox.addActionListener(this);
            categoryCheckbox.setToolTipText(Internationalization.getString("tooltip_category_check_box"));
            this.categoryCheckBoxes.add(categoryCheckbox);
            this.add(categoryCheckbox);
        }
    }

    public void refresh() {
        this.removeAll();
        initComponents();
        this.repaint();
        this.categoriesSelectionChanged();
        this.prioritiesSelectionChanged();
    }

    public void actionPerformed(ActionEvent e) {
        // TODO: action when a checkbox is clicked
        Object o = e.getSource();
        if (o instanceof CategoryCheckBox && !isPriorityCheckbox(o)) {
            categoriesSelectionChanged();
        } else if (isPriorityCheckbox(o)) {
            prioritiesSelectionChanged();
        }
    }

    private boolean isPriorityCheckbox(Object o) {
        return o.equals(highPriorityCheckBox) || o.equals(normalPriorityCheckBox);
    }

    private void categoriesSelectionChanged() {
        ArrayList<String> categoriesSelected = new ArrayList<String>();
        for (JCheckBox jCheckBox : categoryCheckBoxes) {
            if (jCheckBox.isSelected()) {
                categoriesSelected.add(jCheckBox.getText());
            }
        }
        viewPanel.showEntriesByCategories(categoriesSelected);
    }
    
    private void prioritiesSelectionChanged() {
        ArrayList<String> priorities = new ArrayList<String>();
        if (highPriorityCheckBox.isSelected()) {
            priorities.add(highPriorityCheckBox.getPriority());
        }
        if (normalPriorityCheckBox.isSelected()) {
            priorities.add(normalPriorityCheckBox.getPriority());
        }
        viewPanel.showEntriesByPriorities(priorities);
    }
}
